<?php

namespace app\Admin\controller;

use app\Admin\model\Images;
use think\Controller;
use think\Db;
use think\Request;

class Good extends Controller
{
    public function getSelect()
    {
        $pid = input('id', 0);
        $data = \app\Admin\model\Type::where('pid', $pid)->select();
        return json($data);
    }

    public function getBrand()
    {
        $tid = input('id');
        $data = \app\Admin\model\Brand::where('tid', $tid)->select();
        return json($data);
    }

    public function toadd()
    {
        return view('add');
    }

    public function upload()
    {

    }


    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $total = collection(\app\Admin\model\Good::all())->toArray();
        $data = \app\Admin\model\Good::paginate(2);
        $brand = collection(\app\Admin\model\Brand::all())->toArray();
        $type = collection(\app\Admin\model\Type::all())->toArray();
        $count = count($total);
        return  view('show',['data'=>$data,'brand'=>$brand,'type'=>$type,'count'=>$count]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $params = $request->except('/admin/good/save_html');
        $good = new \app\Admin\model\Good();
        $good_id =  $good->addGood($params);
        $files = request()->file('images');
        $images = [];
        foreach ($files as $file) {
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if ($info) {
                $images[] = DS . 'public' . DS . 'uploads' . DS . $info->getFilename();
            } else {
                dump($file->getError());die;
            }
        }
        $img = new Images();
        foreach ($images as $image){
              $row = [
                  'good_id'=>$good_id,
                  'good_iamges'=>$image
              ];
              $img->addImg($row);
        }
        return redirect('index');
    }


    /**
     * 显示指定的资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
